package com.yk.silence.rxretrofitmvp.base;

import android.app.Application;
import android.content.Context;

import com.yk.silence.rxretrofitmvp.common.AppLifeCycleCallBack;
import com.yk.silence.rxretrofitmvp.common.Constants;
import com.yk.silence.rxretrofitmvp.common.CrashHandler;


public class APP extends Application {

    private static APP mApp = null;
    private Context mContext;

    @Override
    public void onCreate() {
        super.onCreate();
        // AppLifecycleCallback 实现 ActivityLifecycleCallbacks 接口方法
        this.registerActivityLifecycleCallbacks(new AppLifeCycleCallBack());
        mContext = this;
        CrashHandler.getInstance().init(getApplicationContext(), Constants.LOG_PATH);
    }

    private APP() {
    }

    /**
     * 单例
     *
     * @return
     */
    public static APP getInstance() {
        if (mApp == null) {
            synchronized (APP.class) {
                if (mApp == null) {
                    mApp = new APP();
                }
            }
        }
        return mApp;
    }

    /**
     * 获取上下文
     * @return
     */
    public Context getContext() {
        if (mContext != null) {
            return mContext;
        } else {
            return getApplicationContext();
        }
    }
}
